package com.moji.controller;

import io.swagger.annotations.Api;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.io.StringWriter;

@RestController
@Api(tags = "配置文件", value = "配置文件管理接口")
public class XmlViewController {
    private final Resource xmlResource = new ClassPathResource("sitemap.xml");

    @GetMapping(value = "/web/sitemap", produces = MediaType.APPLICATION_XML_VALUE)
    public String showXml() throws Exception {
        Source xmlSource = new StreamSource(xmlResource.getInputStream());
        StringWriter writer = new StringWriter();
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        transformer.transform(xmlSource, new StreamResult(writer));
        return writer.toString();
    }
}
